#!/usr/bin/env python
# -*- coding: utf-8 -*-

# libgpdonkey .... utils from gpdonkey here

import commands
import urllib2
import re
import sets

class gpdonkey:

	def gethtml(self, URL):
		web = urllib2.urlopen(URL)
		html = web.read()
		web.close()	
		return html
	
	def parsehtml(self, html):
		
		links = re.findall('<A HREF="peliculas/.*?>',html)
		i = 0
		for line in links:	
			link2 = re.sub('(<A HREF="|">)','',line)
			links[i] = link2
			i=i+1
		links = list(set(links)) #Limpiara valores repetidos de la lista
		return links
	
	def parsed2k(self, links):
		
		x = 0
		url = "http://www.elitefreak.net/"
		tempfil = ['Links']
		for n in links:
			urlt = url + n
			htmltemp = self.gethtml(urlt)
			ed2klink = re.findall('HREF="ed.*?"',htmltemp)
			for line in ed2klink:
				ed2k = re.sub('(HREF="|")','',line)
				#print ed2k
				tempfil = tempfil + [ed2k]
				x = x + 1
	
		return tempfil
	
	def createfitxer(self, ed2klinks):
		
		fd = open("TEMPED2K","w")
		for line in ed2klinks:
			fd.write(line+'\n')
			#datos = line.split("\n")
	   		#cmd="cat TEMPED2K | grep "+ datos[0]
	   		#cmd = ""
	   		#rtn = commands.getoutput(cmd) 
	   		#print datos[0]
		fd.close()
		
		
	#def main():
		
		#URL="http://www.elitefreak.net"
		#print "Getting URL...."+URL
		#html = gethtml(URL)
		#print "Parsing all html links in index.html...."
		#links = parsehtml(html)
		#print "Extracting all ed2k links...."
		#edk2links = parsed2k(links)
		#print "Creating File...TEMPED2K"
		#createfitxer(edk2links)
		#print "End"
		
	#main()
        